#include "Comm.hpp"
#include "Fifo.hpp"
#include <unistd.h>

int main()
{
    // 1. 获取key
    key_t key = GetShmKeyOrDie();
    cout << "key:" << ToHex(key) << endl;
    // 2. 获取共享内存
    int shmid = GetShm(key, defaultsize);
    cout << "shmid: " << shmid << endl;
    // 3. 将共享内存和进程进行挂接(关联)
    char *addr = (char *)ShmAttach(shmid);
    cout << "Attach shm success, addr:" << ToHex((uint64_t)addr) << endl;
    // 4.可以进行通信了
    Sync syn;
    syn.OpenWriteOrDie();
    memset(addr, 0, defaultsize);
    for (char c = 'A'; c <= 'Z'; c++) // pipe, fifo, ->read/write->系统调用， shm -> 没有使用系统调用！！
    {
        addr[c - 'A'] = c;
        sleep(1);
        syn.Wakeup();
    }
    // 5.通信完毕，去关联，休息5秒，客户端退出
    ShmDetach(addr);
    cout<<"Detach shm success, addr:"<<ToHex((uint64_t)addr)<<endl;
    sleep(5);
    return 0;
}
